﻿package {
	//可视化相关类
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	//色彩类
	import flash.geom.ColorTransform;
	import flash.geom.Transform;
	//事件类
	import flash.utils.Timer;
	import flash.events.MouseEvent;
	import flash.events.Event;
	//加载相关类
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.display.Loader;
	//文本相关类
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	//系统类
	import flash.system.System;
	//矩形类
	import flash.geom.Rectangle;
	//播放器核心类
	import KingPlay;
	//歌词显示类
	import LRC;
	//进度条类
	import PlayBar;
	//媒体列表类
	import MusicList;
	//滚动条类
	import Scroll;
	//声波类
	import MusicWave;

	//类及变量声明
	//======================================================================================//
	public class KingPlayer extends MovieClip {
		//列表文件 
		private var loadURL:URLRequest;
		//列表加载对象
		private var listURLLoader:URLLoader;
		//列表XML
		private var listXML:XML;
		//列表数组
		private var listArr:Array=new Array;
		//播放器
		private var kingPlay:KingPlay;
		//歌词
		private var Lrc:LRC;
		//进度条 
		private var playBar:PlayBar;
		//列表
		private var musicList:MusicList;
		//对界面的引用
		private var ui:MovieClip;
		//背景颜色
		private var color:ColorTransform;
		//初始列表高
		private var listHeight:Number;
		//是否静音
		private var isVolumeClose:Boolean=false;
		//静音记忆
		private var volumeX:Number;

		//======================================================================================//
		//构造函数
		public function KingPlayer() {
			init();
		}
		//初始化
		private function init() {
			//解决编码问题
			System.useCodePage=true;
			kingPlay=new KingPlay();
		}
		//
		//======================================================================================//
		/**
		//提供给外部的方法
		**/
		//加载歌曲XML列表URL
		public function load(url:String):void {
			loadURL=new URLRequest(url);
			listURLLoader=new URLLoader  ;
			listURLLoader.load(loadURL);
			listURLLoader.addEventListener(Event.COMPLETE,loadEnd);
		}
		// 
		//======================================================================================//
		/**
		以下为类内部函数
		**/
		//================================================》
		//加载列表文件成功
		private function loadEnd(e:Event) {
			listXML=new XML(listURLLoader.data);
			for (var i:int=0; i < listXML.music.length(); i++) {
				var Obj:Object=new Object;
				Obj.name=listXML.music[i].@name;
				Obj.url=listXML.music[i].@url;
				Obj.lrc=listXML.music[i].@lrc;
				listArr.push(Obj);
			}
			listPlay(listArr);
		}
		//================================================》
		//加载列表文件成功后初始化各类
		private function listPlay(listArr:Array):void {
			kingPlay.listArray=listArr;
			kingPlay.addEventListener(kingPlay.LOADPALY,LoadPlay);
			Lrc=new LRC();
			Lrc.listLRC(kingPlay.sound,kingPlay.channel,kingPlay.lrc);
			loaderUI();
		}
		//================================================》
		//当播放器核心类成功播放媒体后
		private function LoadPlay(e:Event) {
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
			Lrc.listLRC(kingPlay.sound,kingPlay.channel,kingPlay.lrc);
			musicList.upList(kingPlay.playID);
			Lrc.reviseTimeLRC=0;
		}
		//================================================》
		//加载界面
		private function loaderUI() {
			var url:URLRequest=new URLRequest("UI.swf");
			var UIloader:Loader=new Loader;
			UIloader.load(url);
			UIloader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadUI);
		}
		//================================================》
		//生成界面并设置界面元素的控制事件
		private function loadUI(e:Event) {
			ui=e.target.content;
			ui.UI.x=0;
			ui.UI.y=0;
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//生成列表
			musicList=new MusicList();
			musicList.list=listArr;
			musicList.addEventListener(musicList.MUSIC_CLICK,MusicClick);
			ui.UI.musicListAll_MC.musicList_MC.musicListBody_MC.addChild(musicList);
			listHeight=ui.UI.musicListAll_MC.height;
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//生成滚动条
			var scrolls=new Scroll();
			scrolls.mc=ui.UI.musicListAll_MC.musicList_MC.musicListBody_MC;
			scrolls.scrollWidth=ui.UI.musicListAll_MC.musicList_MC.musicListBody_MC.width;
			scrolls.scrollHeigth=ui.UI.musicListAll_MC.musicList_MC.musicListBody_MC.height;
			ui.UI.musicListAll_MC.musicList_MC.addChild(scrolls);
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//显示歌词
			ui.UI.lrc_MC.lrc.addChild(Lrc);
			Lrc.LRCwidth=ui.UI.lrc_MC.lrc.width;
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//显示进度条
			playBar=new PlayBar();
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
			playBar.Width=ui.UI.loadBar_MC.loadBar.width;
			playBar.y+=ui.UI.loadBar_MC.loadBar.height/2;
			ui.UI.loadBar_MC.loadBar.addEventListener(MouseEvent.MOUSE_DOWN,loadBar_MouseDown);
			ui.UI.loadBar_MC.loadBar.addChild(playBar);
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//显示歌曲播放时间
			ui.UI.playTime_MC.playTime.addChild(Lrc.getPlayTime());
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//波形图
			var musicWaves:MusicWave=new MusicWave(ui.UI.musicWave_MC.width,ui.UI.musicWave_MC.height);
			ui.UI.musicWave_MC.musicWave.addChild(musicWaves);
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//歌曲名
			ui.UI.playName_MC.playName.addChild(kingPlay.getPlayName());
			//－－－－－－－－－－－－－－－－－－－－－－－－－－>
			//按钮事件
			btn();
			//工具提示
			tishi();
			//背景颜色
			setColor();
			addChild(ui);
		}
		//================================================》
		//进度条点击事件
		private function loadBar_MouseDown(e:Event) {
			var playPoint:Number=(e.currentTarget.mouseX/e.currentTarget.width)*playBar.soundLength;
			kingPlay.goto(playPoint);
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
			Lrc.listLRC(kingPlay.sound,kingPlay.channel,kingPlay.lrc);
		}
		//================================================》
		//媒体列表点击事件
		private function MusicClick(e:Event) {
			kingPlay.load(e.target.musicURL);
		}
		//================================================》
		//界面各按扭点击事件
		private function btn() {
			//媒体播放控制
			ui.UI.play_btn.addEventListener(MouseEvent.MOUSE_DOWN,plays);
			ui.UI.pause_btn.addEventListener(MouseEvent.MOUSE_DOWN,pauses);
			ui.UI.stop_btn.addEventListener(MouseEvent.MOUSE_DOWN,stops);
			ui.UI.last_btn.addEventListener(MouseEvent.MOUSE_DOWN,lasts);
			ui.UI.next_btn.addEventListener(MouseEvent.MOUSE_DOWN,nexts);
			//列表循环控制
			ui.UI.musicListAll_MC.cycOne_btn.addEventListener(MouseEvent.MOUSE_DOWN,cycOne);
			ui.UI.musicListAll_MC.cycAll_btn.addEventListener(MouseEvent.MOUSE_DOWN,cycAll);
			ui.UI.musicListAll_MC.autoRom_btn.addEventListener(MouseEvent.MOUSE_DOWN,autoRom);
			ui.UI.musicListAll_MC.autoNext_btn.addEventListener(MouseEvent.MOUSE_DOWN,autoNext);
			//音量控制
			ui.UI.volume.volumeSlideMC.addEventListener(MouseEvent.MOUSE_DOWN,volumeMouseDown);
			ui.UI.volume.volumeSlideMC.x=ui.UI.volume.volumeMC.width/2;
			ui.UI.volume.volumeSlideMC.addEventListener(Event.ENTER_FRAME,volumeEnterFrame);
			ui.UI.volumeClose_btn.addEventListener(MouseEvent.MOUSE_DOWN,volumeClose);
			//歌词风格及时间修正控制
			ui.UI.LRCstyle0_btn.addEventListener(MouseEvent.MOUSE_DOWN,LRCstyle0);
			ui.UI.LRCstyle1_btn.addEventListener(MouseEvent.MOUSE_DOWN,LRCstyle1);
			ui.UI.addTime_btn.addEventListener(MouseEvent.MOUSE_DOWN,addTime);
			ui.UI.jTime_btn.addEventListener(MouseEvent.MOUSE_DOWN,jTime);
		}
		//================================================》
		//功能提示
		private function tishi() {
			ui.UI.foor_mc.tishi_txt.alpha=.5;
			ui.UI.jTime_btn.addEventListener(MouseEvent.MOUSE_OVER,jTime_tishi);//歌词减慢
			function jTime_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌词修正：滞后显示";
			}
			ui.UI.addTime_btn.addEventListener(MouseEvent.MOUSE_OVER,addTime_tishi);//歌词加快
			function addTime_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌词修正：加快显示";
			}
			ui.UI.LRCstyle0_btn.addEventListener(MouseEvent.MOUSE_OVER,LRCstyle0_tishi);//歌词水平
			function LRCstyle0_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌词水平卡拉OK";
			}
			ui.UI.LRCstyle1_btn.addEventListener(MouseEvent.MOUSE_OVER,LRCstyle1_tishi);//歌词垂直
			function LRCstyle1_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌词垂直滚动";
			}
			ui.UI.musicListAll_MC.autoNext_btn.addEventListener(MouseEvent.MOUSE_OVER,autoNext_tishi);//顺序播放
			function autoNext_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌曲顺序播放";
			}
			ui.UI.musicListAll_MC.autoRom_btn.addEventListener(MouseEvent.MOUSE_OVER,autoRom_tishi);//随机播放
			function autoRom_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌曲随机播放";
			}
			ui.UI.musicListAll_MC.cycAll_btn.addEventListener(MouseEvent.MOUSE_OVER,cycAll_tishi);//列表循环
			function cycAll_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌曲列表循环";
			}
			ui.UI.musicListAll_MC.cycOne_btn.addEventListener(MouseEvent.MOUSE_OVER,cycOne_tishi);//单曲循环
			function cycOne_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="歌曲单曲循环";
			}
			ui.UI.foor_mc.color_btn.addEventListener(MouseEvent.MOUSE_OVER,color_tishi);//
			function color_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="播放器颜色随心变";
			}
			ui.UI.foor_mc.colors_btn.addEventListener(MouseEvent.MOUSE_OVER,colors_tishi);//
			function colors_tishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="恢复播放器颜色";
			}
			ui.UI.foor_mc.listUI_btn.addEventListener(MouseEvent.MOUSE_OVER,listUI_btns);//
			function listUI_btns(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="展开/收起播放列表";
			}
			ui.UI.notishi_btn.useHandCursor=false;
			ui.UI.notishi_btn.addEventListener(MouseEvent.MOUSE_OVER,notishi);//取消提示
			function notishi(e:MouseEvent) {
				ui.UI.foor_mc.tishi_txt.text="功能提示";
			}
			//================================================》
			//收缩界面
			ui.UI.foor_mc.listUI_btn.addEventListener(MouseEvent.MOUSE_DOWN,listUI_btn);//橙色
			function listUI_btn(e:MouseEvent) {
				var hh:Number;
				if (ui.UI.musicListAll_MC.height>0) {
					ui.UI.musicListAll_MC.scaleY=0;
					hh=0;
				} else {
					ui.UI.musicListAll_MC.scaleY=1;
					hh=listHeight;
				}
				ui.UI.bg_mc.height=ui.UI.top_mc.height+ui.UI.foor_mc.height+hh;
				ui.UI.foor_mc.y=ui.UI.top_mc.y+ui.UI.top_mc.height+hh;
			}
		}
		//================================================》
		//变换背景
		private function setColor() {
			ui.UI.foor_mc.color_btn.addEventListener(MouseEvent.MOUSE_DOWN,colors);//变色
			function colors(e:MouseEvent) {
				color=ui.UI.bg_mc.transform.colorTransform;
				color.color=Math.floor(0xFFFFFF*Math.random());
				ui.UI.bg_mc.transform.colorTransform=color;
			}
			ui.UI.foor_mc.colors_btn.addEventListener(MouseEvent.MOUSE_DOWN,colorss);//恢复
			function colorss(e:MouseEvent) {
				ui.UI.bg_mc.transform.colorTransform=new ColorTransform()
			}
		}

		//================================================》
		//列表单曲循环
		private function cycOne(evt:MouseEvent) {
			kingPlay.cycMode=2;
			ui.UI.musicListAll_MC.cycMC.gotoAndStop(2);
		}
		//列表所有循环
		private function cycAll(evt:MouseEvent) {
			kingPlay.cycMode=1;
			ui.UI.musicListAll_MC.cycMC.gotoAndStop(1);
		}
		//随机播放
		private function autoRom(evt:MouseEvent) {
			kingPlay.autoMode=2;
			ui.UI.musicListAll_MC.autoPlayMC.gotoAndStop(2);
		}
		//顺序播放
		private function autoNext(evt:MouseEvent) {
			kingPlay.autoMode=1;
			ui.UI.musicListAll_MC.autoPlayMC.gotoAndStop(1);
		}
		//================================================》
		//设置音量
		private function volumeMouseDown(evt:MouseEvent) {
			var Rect:Rectangle = new Rectangle(ui.UI.volume.volumeMC.x,ui.UI.volume.volumeSlideMC.y,ui.UI.volume.volumeMC.width,0);
			ui.UI.volume.volumeSlideMC.startDrag(true,Rect);
		}
		private function volumeEnterFrame(evt:Event) {
			var _volume:Number=ui.UI.volume.volumeSlideMC.x/ui.UI.volume.volumeMC.width;
			kingPlay.setVolume(_volume);
		}
		private function volumeClose(evt:MouseEvent) {
			if (!isVolumeClose) {
				volumeX=ui.UI.volume.volumeSlideMC.x;
				ui.UI.volume.volumeSlideMC.x=0;
				ui.UI.volumeClose_mc.gotoAndStop(2);
			} else {
				ui.UI.volume.volumeSlideMC.x=volumeX;
				ui.UI.volumeClose_mc.gotoAndStop(1);
			}
			isVolumeClose=!isVolumeClose
		}
		//================================================》
		//歌词修正
		private function addTime(evt:MouseEvent) {
			Lrc.reviseTimeLRC-=1;
		}
		private function jTime(evt:MouseEvent) {
			Lrc.reviseTimeLRC+=1;
		}
		//================================================》
		//歌词风格
		private function LRCstyle0(evt:MouseEvent) {
			Lrc.LRCstyle=0;
		}
		private function LRCstyle1(evt:MouseEvent) {
			Lrc.LRCstyle=1;
		}
		//================================================》
		//播放控制
		private function plays(evt:MouseEvent) {
			kingPlay.play();
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
			Lrc.listLRC(kingPlay.sound,kingPlay.channel,kingPlay.lrc);
		}
		private function pauses(evt:MouseEvent) {
			kingPlay.pause();
			Lrc.txt="歌曲暂停中。。。";
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
		}
		private function stops(evt:MouseEvent) {
			kingPlay.stop();
			Lrc.txt="停止播放";
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
		}
		private function lasts(evt:MouseEvent) {
			kingPlay.last();
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
		}
		private function nexts(evt:MouseEvent) {
			kingPlay.next();
			playBar.sound=kingPlay.sound;
			playBar.channel=kingPlay.channel;
		}

	}
}